var tmpFieldType; //临时存放字段类型
$(function() {
	// 字段类型下拉框
	sys.selectpicker({
		id : 'fieldName',
		async: false,
		url : rootPath + '/customerCustomField/listCustomerCustomField',
		valueField : 'id',
		textField : 'fieldName',
		defaultValue : $('#e_fieldId').val(),
	})
	//生成选项值操作框
	generateChoiceOption($('#fieldName').val());
	//字段类型下拉框change事件
    $('#fieldName').on('changed.bs.select', function (e) {
		var value = $(this).selectpicker('val');
		generateChoiceOption(value);
	});
	//点击提交事件
	$("#btn_save").click(function() {
		var index = parent.layer.getFrameIndex(window.name);
		var fieldId = $('#fieldName').val();
		var fieldName = $('#fieldName').find("option:selected").text();
		var fieldValue = "";
		if (tmpFieldType == "1") { //文本
			fieldValue = $('#fieldValue1').val();
		} else if (tmpFieldType == "2") { //单选
			fieldValue = $('#fieldValue2').selectpicker('val');
		} else if (tmpFieldType == "3") { //复选
			fieldValue = $('#fieldValue3').selectpicker('val');
		} 
		if (fieldValue) {
			parent.$('#tmpFieldId').val(fieldId);
			parent.$('#tmpFieldType').val(tmpFieldType);
			parent.$('#tmpFieldName').val(fieldName);
			parent.$('#tmpFieldValue').val(fieldValue);
			parent.$('#layer_opt_status').val('1');
			parent.layer.close(index);
		} else {
			parent.$('#layer_opt_status').val('0');
			showErr('请填写值！');
		}
		return false;
	});
	//点击返回事件
	$("#btn_back").click(function() {
		var index = parent.layer.getFrameIndex(window.name);
   		parent.$('#layer_opt_status').val('0'); 
   		parent.layer.close(index);
	});
	
});

/**
 * 生成选项值操作框
 * @param id 自定义属性ID 
 */
function generateChoiceOption(id){
	var editFieldValue = $('#e_fieldValue').val(); //如果是编辑状态，则获取要编辑的值
	$.ajax({
		type: 'POST',
		url: rootPath + '/customerCustomField/getCustomerCustomField',
		data: {id: id},
		dataType: 'json',
		success: function(data) {
			if (data) { 
				var choiceOption = [];
				var choiceOptionStr = data.choiceOption;
				if (choiceOptionStr) {
					choiceOption = eval(choiceOptionStr);
				}
				$('.fieldTypeContent').hide();
				tmpFieldType = data.fieldType;
				if (tmpFieldType == "1") { //文本
					$('#inputType').show(); 
					if (editFieldValue) {
						$('#fieldValue1').val(editFieldValue);
					}
				} else if (tmpFieldType == "2") { //单选
					$('#fieldValue2').empty();
					if (choiceOptionStr) {
						for (var i = 0; i < choiceOption.length; i++) {
							$('#fieldValue2').append('<option value="'+choiceOption[i].key+'" >'+choiceOption[i].value+'</option>');
						}
						//设置默认值
						if (editFieldValue) {
							$('#fieldValue2').selectpicker('val', editFieldValue);
						}
						$('#fieldValue2').selectpicker('refresh');
					}
					$('#singleSelectType').show();
				} else if (tmpFieldType == "3") { //复选
					$('#fieldValue3').empty();
					if (choiceOptionStr) {
						for (var i = 0; i < choiceOption.length; i++) {
							$('#fieldValue3').append('<option value="'+choiceOption[i].key+'" >'+choiceOption[i].value+'</option>');
						}
						//设置默认值
						if (editFieldValue) {
							$('#fieldValue3').selectpicker('val', editFieldValue);
						}
						$('#fieldValue3').selectpicker('refresh');
					}
					$('#multiSelectType').show();
				} 
			}
		}
	})
}


